let db = require('../db/index')

exports.allNotice = (req, res) => {        //获取info表全部数据
    var sql = 'select * from notice'
    db.query(sql, (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        res.send(data)
    })
}

exports.get = (req, res) => {        //通过id查询数据
    var sql = 'select * from notice where notice_id = ?'    //？用于占位
    db.query(sql, [req.query.notice_id], (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        res.send(data)
    })
}

exports.addNotice = (req, res) => {        //向info表添加数据
    var sql = 'insert into notice (user_id,notice_content,notice_time) values (?,?,?)'
    db.query(sql, [req.query.user_id, req.query.notice_content, req.query.notice_time], (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        if(data.affectedRows > 0) {
            res.send({
              status: 200,
              message: 'success'
            })
        }else{
            res.send({
              status: 202,
              message: 'error'
            })
        }
    })
}



exports.del = (req, res) => {        //通过id删除数据
    var sql = 'delete from notice where notice_id = ?'
    db.query(sql, [req.query.notice_id], (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        if(data.affectedRows > 0) {
            res.send({
              status: 200,
              message: '删除成功'
            })
        }else{
            res.send({
              status: 202,
              message: '删除失败'
            })
        }
    })
}
